/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Web Feeds is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

import QtQuick 1.1
import com.nokia.meego 1.0
import com.nokia.extras 1.1
import WebFeeds 1.0
import GoogleReader 1.0
import "../../../../shared/qml/UIConstants.js" as UIConstants
import "../../../../shared/qml" as Shared
import "../delegates" as Delegates
import "../misc" as Misc
import "../buttons" as Buttons

GoogleReaderPage {
    id: root

    property variant rootIndex
    property bool contentsEnabled: false
    onStatusChanged: {
        if((root.status == PageStatus.Active)
            && !root.contentsEnabled) {
            root.rootIndex = webFeedsApp.pageItemModel.index(root.rootItem);
            root.contentsEnabled = true;
        }
    }

    Component {
        id: feedDelegate
        Delegates.FeedPageListDelegate {
            onReleased: createPageForItem(listPageContents.itemForRow(row));
            onHolded: showContextMenuForItem(listPageContents.itemForRow(row));
        }
    }

    Component {
        id: compoundFeedDelegate
        Delegates.CompoundFeedPageListDelegate {
            service: root.rootItem.service;
            onReleased: {
                if(!root.rootItem.editingBusy) {
                    createPageForItem(listPageContents.itemForRow(row));
                }
            }
            onHolded: {
                if(!root.rootItem.editingBusy)
                    showContextMenuForItem(listPageContents.itemForRow(row));
            }
        }
    }

    SelectionDialog {
        id: filterSelectionDialog
        titleText: qsTr("Show")
        model: ListModel {
            ListElement {name: "All items";}
            ListElement {name: "New items";}
        }
        onStatusChanged: {
            if((filterSelectionDialog.status == DialogStatus.Opening)
               || (filterSelectionDialog.status == DialogStatus.Open)) {
                filterSelectionDialog.selectedIndex = root.service.itemFilter;
            }
        }
        onAccepted: {
            if((filterSelectionDialog.selectedIndex >= 0)
                && (root.service.itemFilter != filterSelectionDialog.selectedIndex)) {
                root.service.itemFilter = filterSelectionDialog.selectedIndex;
                console.log(root.rootItem.title);
                root.service.refreshFeed(root.rootItem);
            }
        }
    }
    Component {
        id: countBubbleComponent
        Misc.HeaderCountBubble {
            busy: root.rootItem.busy
            unreadCount: (root.rootItem.itemType != GoogleReaderItem.ItStarredTagItem) ? root.rootItem.unreadCount : 0
            limit: service.unreadCountLimit
        }
    }

    contents: Shared.CommonListPageContents {
        id: listPageContents
        rootItem: root.rootItem
        rootIndex: root.rootIndex
        enabled: root.contentsEnabled
        busy: root.contentsBusy
        emptyListText: (root.service.itemFilter == GoogleReaderEnum.IftAllItems) ? qsTr("No items") : qsTr("No unread items")
        pageHeader: Shared.CommonPageHeader {
            filterEnabled: true
            onClicked: filterSelectionDialog.open();
            titleText: root.rootItem.title
            titleColor: "white"
            backgrounColor: UIConstants.HIGHLIGHT_COLOR
            titleFont: UIConstants.FONT_FAMILY_LIGHT
            rightHelperItem: countBubbleComponent
            Shared.Separator {
                id: separator
                anchors.bottom: parent.bottom
            }
        }
        delegate: (root.rootItem.itemType == GoogleReaderItem.ItFeedItem) ? feedDelegate : compoundFeedDelegate
    }
    tools: Shared.CommonToolBarLayout {
        enabled: root.userInteractionEnabled
        Shared.BackToolButton {pageItem: root.rootItem; page: root}
        Buttons.MarkEverythingAsReadToolButton {pageItem: root.rootItem}
        Buttons.MarkFeedAsReadToolButton {pageItem: root.rootItem}
        Buttons.RefreshFeedToolButton {pageItem: root.rootItem}
        Shared.MenuToolButton {pageItem: root.rootItem; page: root}
    }
}
